#include "login_window.h"
#include "table_window.h"

#include <QMessageBox>
#include <QRegExpValidator>

LoginWindow::LoginWindow(QWidget *parent,const QString &url)
    : QWidget(parent),url_(url)
{
    setWindowTitle(tr("登录"));
    //    setAttribute(Qt::WA_DeleteOnClose);

    resize(300,200);
    v_container = new QVBoxLayout(this);
    info_container = new QGridLayout();
    label_user_name = new QLabel(this);
    label_user_name->setText(tr("用户名"));

    edit_user_name = new QLineEdit(this);
    edit_user_name->setMaxLength(10);
    QRegExp regx("[a-zA-Z0-9]+$");
    QValidator *validator = new QRegExpValidator(regx, this);

    edit_user_name->setValidator(validator);

    info_container->addWidget(label_user_name,1,1);
    info_container->addWidget(edit_user_name,1,2);

    v_container->addStretch(1);
    v_container->addLayout(info_container);

    v_container->addSpacing(20);

    h_button_container = new QHBoxLayout();
    button_login = new QPushButton(this);
    button_login->setText(tr("登录"));
    button_exit = new QPushButton(this);
    button_exit->setText(tr("退出"));
    h_button_container->addWidget(button_login);
    h_button_container->addWidget(button_exit);

    v_container->addLayout(h_button_container);
    v_container->addStretch(1);

    connect(button_exit, &QPushButton::clicked, this,&LoginWindow::on_exit_click);
    connect(button_login, &QPushButton::clicked, this,&LoginWindow::on_login_click);
}

LoginWindow::~LoginWindow()
{
}

void LoginWindow::on_login_click(bool)
{
    QString user_name = edit_user_name->text();
    if(user_name.length()==0)
    {
        QMessageBox::warning(this,"提示","请输入用户名");
        return;
    }
    TableWindow *w = new TableWindow(nullptr,user_name,url_);
    w->show();
    close();
}

void LoginWindow::on_exit_click()
{
    close();
}

